/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * DataBaseUtil.java
 * wholebasic
 * 2013年10月23日下午5:46:01
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.db;

import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceFactory {

	static final Log logger = LogFactory.getLog(DataSourceFactory.class);
	static PropertiesConfiguration pc;
	static {
		PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
		pc = new PropertiesConfiguration();
		Resource commonResource = rpr.getResource("datapool.properties");
		try {
			pc.load(commonResource.getInputStream());
		} catch (ConfigurationException | IOException e) {
			logger.error(e);
			e.printStackTrace();
		}
	}

	public static DataSource buildDataSource(String props) {

		try {
			return commonDataSource(props);
		} catch (PropertyVetoException | IOException e) {
			// logger.error(e);
			e.printStackTrace();
		}
		return null;
	}

	private static DataSource commonDataSource(String jdbcidt)
			throws PropertyVetoException, IOException {
		ComboPooledDataSource ds = new ComboPooledDataSource(true);
		ds.setDriverClass("com.mysql.jdbc.Driver");
		PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
		Resource xaprops = rpr.getResource(jdbcidt);
		Properties hp = new Properties();
		hp.load(xaprops.getInputStream());
		logger.info("jdbc  properties's uri is " + xaprops.getURI());
		ds.setJdbcUrl(hp.getProperty("url"));
		ds.setUser(hp.getProperty("user"));
		ds.setPreferredTestQuery("select 1");
		ds.setIdleConnectionTestPeriod(18000);
		ds.setAutomaticTestTable("c3p0_test");
		ds.setTestConnectionOnCheckout(true);
		ds.setPassword(hp.getProperty("password"));
		ds.setMinPoolSize(pc.getInt("minSize"));
		ds.setMaxPoolSize(pc.getInt("maxSize"));
		return ds;
	}	
}
